package com.example.studyPlatform.mapper;

import com.example.studyPlatform.bean.Post;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PostMapper {

    @Insert("insert into posts " +
            "(type,author,create_time,update_time,disallow_comment,likes," +
            "meta_description,meta_keywords,content,password,status," +
            "summary,thumbnail,title,top_priority,url,visits,word_count)" +
            " value (#{type},#{author},#{createTime},#{updateTime},#{disallowComment}," +
            "#{likes},#{metaDescription},#{metaKeywords},#{content},#{password}," +
            "#{status},#{summary},#{thumbnail},#{title},#{topPriority},#{url},#{visits},#{wordCount})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int publish(Post post);

    @Select("select * from posts where id = #{id}")
    Post getPostById(int id);

    @Select("select * from posts where " +
            "meta_description LIKE #{key} OR  " +
            "meta_keywords LIKE #{key} OR " +
            "summary LIKE #{key} OR " +
            "title LIKE #{key} " +
            "order by visits DESC limit #{offset},#{size}")
    List<Post> search(String key,int offset,int size);


    @Select("select * from posts order by create_time DESC limit #{offset},#{size}")
    List<Post> sortByDate(int offset,int size);



    @Select("select * from posts order by visits DESC limit #{offset},#{size}")
    List<Post> sortByVisit(int offset,int size);

    @Select("select * from posts where type= #{type} order by create_time DESC limit #{offset},#{size}")
    List<Post> selectByType(int type,int offset,int size);

    @Select("select * from posts where author = #{author} order by create_time DESC limit #{offset},#{size}")
    List<Post> selectByUser(int author,int offset,int size);

    @Select("DELETE FROM posts WHERE id=#{id}")
    int del(int id);

}

